import Utils from 'src/utils/Utils';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { Controller, Get, Post, Body, Patch, Param, Delete, Req, Query } from '@nestjs/common';
import { ImageService } from './image.service';
import { CreateImageDto } from './dto/create-image.dto';
import { UpdateImageDto } from './dto/update-image.dto';
import { PublicRoute } from 'src/decorations/PublicRoute';
import { SystemService } from '../system/system.service';

@ApiTags('图片URL存储模块')
@Controller('image')
export class ImageController {
  constructor(
    private readonly imageService: ImageService,
    private readonly systemService: SystemService,
  ) { }

  @ApiOperation({ summary: '存储图片' })
  @Post('add/url')
  create(@Body() createImageDto: CreateImageDto, @Req() request: any) {
    if (!createImageDto.refGuid) throw "保存失败，缺少关联资源~"
    createImageDto.guid = Utils.guid()
    createImageDto.creatorGuid = request?.userinfo?.guid || ''
    return this.imageService.create(createImageDto);
  }

  @ApiOperation({ summary: '通过hash删除图片' })
  @Post('delete/hash')
  deleteImageByGuid(@Body() createImageDto: CreateImageDto) {
    if (!createImageDto.hash) throw "未获取到图片 hash 标识~"
    const { hash } = createImageDto
    this.systemService.deleteImage(hash)
    return this.imageService.deleteImageByGuid(hash)
  }

  @ApiOperation({ summary: '条件查询图片' })
  @Post('find/all')
  getImageByGuid(@Body() createImageDto: CreateImageDto) {
    return this.imageService.findAll(createImageDto);
  }
}
